﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Gmantis.Controls.Toolbar
{
    public class ToolbarSplitButton : ToolbarDropDown
    {
        // Events
        public event RoutedEventHandler Click;

        // Methods
        public ToolbarSplitButton()
        {
            base.DefaultStyleKey = typeof(ToolbarSplitButton);
        }

        public override void OnApplyTemplate()
        {
            RoutedEventHandler handler = null;
            RoutedEventHandler handler2 = null;
            base.OnApplyTemplate();
            ToolbarButton templateChild = base.GetTemplateChild("HeaderButton") as ToolbarButton;
            if (templateChild != null)
            {
                if (handler == null)
                {
                    handler = (s, e) => this.OnClick();
                }
                templateChild.Click += handler;
            }
            templateChild = base.GetTemplateChild("HeaderButtonV") as ToolbarButton;
            if (templateChild != null)
            {
                if (handler2 == null)
                {
                    handler2 = (s, e) => this.OnClick();
                }
                templateChild.Click += handler2;
            }
        }

        private void OnClick()
        {
            if (base.IsDropDownOpen)
            {
                base.IsDropDownOpen = false;
            }
            else if (this.Click != null)
            {
                this.Click(this, new RoutedEventArgs());
            }
        }
    }
}
